package yi.Note.ui;

import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import yi.Note.data.provider.NoteContent;

import yi.Note.R;

/**
 * Created with IntelliJ IDEA.
 * User: b251
 * Date: 13-12-7
 * Time: 下午7:02
 * To change this template use File | Settings | File Templates.
 */
public class NoteListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
    private NoteItemCursorAdapter mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText(getString(R.string.notes_list_empty));
        setHasOptionsMenu(true);
        mAdapter = new NoteItemCursorAdapter(getActivity(),null, true);
        setListAdapter(mAdapter);

        // Start out with a progress indicator.
        setListShown(false);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    //LoaderManager.LoaderCallbacks
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri baseUri = NoteContent.Note.CONTENT_URI;
        return new CursorLoader(getActivity(), baseUri, NoteContent.Note.SELECT_PROJECTIONS, null, null,
                NoteContent.Note.TIMESTAMP + " COLLATE LOCALIZED ASC");
    }

    //LoaderManager.LoaderCallbacks
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);

        // The list should now be shown.
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    //LoaderManager.LoaderCallbacks
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    public class NoteItemCursorAdapter extends CursorAdapter{
        private LayoutInflater mInflater;
        public NoteItemCursorAdapter(Context context, Cursor c, boolean autoRequery) {
            super(context, c, autoRequery);
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return mInflater.inflate(R.layout.note_list_item, parent);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView titleView = (TextView)view.findViewById(R.id.title);
            TextView timeView = (TextView)view.findViewById(R.id.title);
            NoteContent.Note note = NoteContent.getContent(cursor, NoteContent.Note.class);

            titleView.setText(note.mTitle);
            titleView.setText(note.mText);
            view.setTag(note);
        }
    }
}
